/**
 * Created by lional on 16/10/2.
 */

jGame.Button = ccui.Button.extend(/** @lends jGame.Button# */{

    _touchTime:null,
    _badge:null,
    _controller:null,
    _isLongClickEnabled: null,
    _longClickAction: null,
    _longClickName: null,
    _sendLongClick: null,
    _touchCallbackListener: null,

    ctor: function (normalImage, selectedImage, disableImage, texType) {
        this._super(normalImage, selectedImage, disableImage, texType);
        //cc.log("jGame.Button#ctor()");
    },

    onExit: function () {

        this.stopAllActions();
        this.setTouchCallbackName();

        this._touchTime = null;
        this._badge = null;
        this._controller = null;
        this._isLongClickEnabled = null;
        this._longClickAction = null;
        this._longClickName = null;
        this._sendLongClick = null;
        this._touchCallbackListener = null;

        this._super();
        //cc.log("jGame.Button[" + this.name + "]#onExit()");
    },

    setController: function(controller){
        this._controller = controller;
    },

    getController: function(){
        return this._controller;
    },

    _checkLongClickToucheBegan: function (sender, type) {
        if(type == ccui.Widget.TOUCH_BEGAN) {
            sender._touchTime = new Date().getTime();

            var widget = this;
            var longClickCallBack = function (sender) {
                if(widget["_controller"]) {
                    if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](sender, ccui.Widget.TOUCH_ENDED);
                    if(typeof widget["_controller"][widget["_longClickName"]] === "function")widget["_controller"][widget["_longClickName"]](sender, ccui.Widget.TOUCH_ENDED);
                    widget._sendLongClick = true;
                    widget._longClickAction = null;
                }
            }
            this._longClickAction = cc.sequence(
                cc.delayTime(jGame.LONG_CLICK_TIME/1000),
                cc.callFunc(longClickCallBack, this)  // If you want to pass a optional value, like "Hi!", then you should pass 'this' too
            );
            this.runAction(this._longClickAction);
            widget._sendLongClick = false;
        }else if(type == ccui.Widget.TOUCH_ENDED) {
            setTimeout(function () {
                sender._touchTime = 0;
            }, 0)
        }
    },

    isLongClick: function () {
        if(this._touchTime) {
            var timestamp = new Date().getTime();
            if(timestamp - this._touchTime > jGame.LONG_CLICK_TIME) {
                return true;
            }
        }
        return false;
    },

    setLongClickEnabled: function (enabled) {
        this._isLongClickEnabled = enabled?true:false;
        if(this._isLongClickEnabled && !this._touchCallbackListener) {
            this.setTouchCallbackName("_checkLongClickToucheBegan");
        }
    },

    setTouchCallbackName: function (callBackName) {
        if(callBackName === undefined)
        {
            this._touchCallbackListener = callBackName;
            this.addTouchEventListener(callBackName);
            return;
        }
        var widget = this;
        this._touchCallbackListener = function(sender, type){
                if(type == ccui.Widget.TOUCH_BEGAN) {
                    if (typeof widget["_controller"] === "object" && widget._isLongClickEnabled && typeof widget["_controller"]["_checkLongClickToucheBegan"] === "function") {
                        widget["_controller"]["_checkLongClickToucheBegan"](sender, type);
                    }
                }else if(type == ccui.Widget.TOUCH_MOVED) {
                    if(widget._longClickAction) {
                        widget.stopAction(widget._longClickAction);
                        widget._longClickAction = null;
                    }
                }
                if(callBackName != "_checkLongClickToucheBegan" && typeof widget["_controller"] === "object" && typeof widget["_controller"][callBackName] === "function"){
                    if(typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](sender, type);
                    widget["_controller"][callBackName](sender, type);
                }
                else if(typeof widget[callBackName] === "function")
                    widget[callBackName](sender, type);
        };
        this.setTouchEnabled(true);
        this.addTouchEventListener(this._touchCallbackListener);
    },

    setClickCallbackName: function (callBackName) {
        this._longClickName = callBackName;
        var widget = this;
        var callBack = function(sender){
            if(widget._sendLongClick) {
                widget._sendLongClick = false;
            }else {
                if (typeof widget["_controller"] === "object" && typeof widget["_controller"][callBackName] === "function") {
                    if (typeof widget["_controller"]["trace"] === "function")widget["_controller"]["trace"](sender);
                    widget.stopAllActions();
                    widget["_controller"][callBackName](sender);
                }
                else if (typeof widget[callBackName] === "function")
                    widget[callBackName](sender);
            }
        };
        this.addClickEventListener(callBack);
    },

    addBadge: function (count) {
        if(typeof this._badge === "number" && count === "number") {
            this._badge += count;
        } else {
            this._badge = count;
        }
    },

    setBadge: function (count) {
        this._badge = count;
    },
    
    getBadge: function () {
        return this._badge;
    },

    _createJGameInstance: function () {
        return new jGame.Button();
    },

    _copyJGameProperties: function (model) {
        this._copyProperties(model);
        this.setBadge(model.getBadge());
        this.setController(model.getController());
    },

    _copyJGameChildren: function (model) {
        var widgetChildren = model.getChildren();
        for (var i = 0; i < widgetChildren.length; i++) {
            var locChild = widgetChildren[i];
            if (locChild instanceof cc.Node)
                this.addChild(jGame.clone(locChild));
        }
    },

    /**
     * Sets title text to ccui.Button
     * @param {String} text
     */
    setTitleText: function (text) {
        if(text === undefined)return;
        this._super(jGame.translate(text));
    },

});